/*
 *      net-sim - link
 *
 * 		Linkreceiver.java
 *
 *      Copyright 23/12/2008 nicolas
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 3 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
package link;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import exceptions.NodeException;

/**
 * The Class Linkreceiver.
 * acepta frames (via UDP) provenientes de la otra punta del link (esto cambiara para links
 * de acceso multiple) y los entrega a la interfaz, quien se encargara de desencapsular y determinar
 * a quien debe pasarlos (IP, ARP, etc)
 */
public class Linkreceiver extends Thread  {

  /** The socket. */
  static DatagramSocket socket = null;

  /** The dgpacket. */
  DatagramPacket dgpacket;

  /** The buff. */
  byte buff[];

  /** The lnk. */
  Link lnk;

  /** Puerto Remoto.Esto podria cambiar si es un link de acceso multiple . */
  static int remport;

  /** The remaddr. */
  static InetAddress remaddr;

  /**
   * Instantiates a new linkreceiver.
   *
   * @param l the l
   * @param sk the sk
   *
   * @throws NodeException the node exception
   */
  public Linkreceiver(Link l, DatagramSocket sk) throws NodeException {
    socket = sk;
    lnk = l;
    buff = new byte[20000];
    dgpacket = new DatagramPacket(buff, 20000);
    this.start();
  }

  /* (non-Javadoc)
   * @see java.lang.Thread#run()
   */
  public void run() {
    try {
      while (true) {
        socket.receive(dgpacket);
        byte[] bb = new byte[dgpacket.getLength()];
        System.arraycopy(buff, 0 , bb, 0, dgpacket.getLength());
        // aca entregar frame al Link, que lo entrega a la interfaz
            System.out.println("LINK recibe  "+ bb);

        lnk.receive(bb);
        //System.out.println("   R<-- (" );
      }
    }
    catch (SocketException se) {
      if (se.getMessage().matches("Socket closed"))
        System.out.println("Linkreceiver closed");
      else {
        //System.out.println("Exception in DCCPreceiver.");
        se.printStackTrace();
      }
    }
    catch (IOException ioex) {
      //System.out.println("Exception in DCCPreceiver.");
      ioex.printStackTrace();
      //aca mandar la exception
    }
    //System.out.println("DCCPreceiver thread finished.");
  }

  /**
   * Close.
   *
   * @throws IOException Signals that an I/O exception has occurred.
   */
  public void close() throws IOException {
    socket.close();
  }
}
